package com.lp.RSTX.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;

@Slf4j
public class UploadUtil {


    private UploadUtil(){}

    public static File[] resolveCompressUploadFile(HttpServletRequest request, MultipartFile file, String path) {

        try {
            request.setCharacterEncoding("utf-8");
            /* 截取后缀名 */
            if (file.isEmpty()) {
                log.info("文件不能为空");
            }
            log.info("1.####" + file.getOriginalFilename());
            String fileName = file.getOriginalFilename();
            log.info(fileName);
            fileName = URLEncoder.encode(fileName, "UTF-8");
            log.info("2.#####fileName" + fileName);
            int pos = fileName.lastIndexOf(".");
            log.info("pos");
            String extName = fileName.substring(pos + 1).toLowerCase();

            log.info("3.####" + extName);
            // 判断上传文件必须是zip否则不允许上传
            if (!extName.equals("zip")) {
                log.info("上传文件格式错误，请重新上传");
            }
            // 时间加后缀名保存
            String saveName = "待解压文件" + "." + extName;
            log.info("4.####");
            // 根据服务器的文件保存地址和原文件名创建目录文件全路径
            File pushFile = new File(path + "/" + saveName);
            log.info(pushFile.toString());
            File descFile = new File(path);
            log.info(descFile.toString());
            if (!descFile.exists()) {
                descFile.mkdirs();
                log.info("5.####文件创建成功");
            }
            // 存放目的文件
            log.info("6.####desc:" + path);
            file.transferTo(pushFile);
            log.info("a.####");
            // 开始解压zip
            if (extName.equals("zip")) {
                CompressFileUtils.zipFile(path, path + saveName);
                log.info("12.####解压成功");
            } else {
                log.info("文件格式不正确不能解压");
            }
            // 删除 解压完的压缩文件
            doDeleteEmptyDir(path + saveName);
            log.info("13.删除文件成功");
            return returnFiles(path + "");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return returnFiles(path + "");
    }

    //返回 File[]
    private static File[] returnFiles(String strPath) {
        File dir = new File(strPath);
        log.info(dir.toString());
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                log.info(fileName);
                File f = new File(strPath + File.separator + fileName);
                log.info("文件名称：" + fileName);
                if (f.isDirectory()) {// 如果f是目录
                    return f.listFiles();
                }
            }
        }
        return files;
    }

    //删除指定文件
    private static void doDeleteEmptyDir(String dir) {
        File file=new File(dir);
        try {
            Files.delete(Paths.get(file.toString()));
            log.info("Successfully deleted empty directory: " + dir);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
